package com.dhcc.mybatis.blh.user;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.dhcc.framework.app.blh.BaseAbstractBlh;
import com.dhcc.framework.common.BaseConstants;
import com.dhcc.framework.exception.DataBaseException;
import com.dhcc.framework.transmission.dto.BaseAbstractDto;
import com.dhcc.mybatis.dto.user.UserDto;
import com.dhcc.mybatis.entity.user.User;
import com.dhcc.mybatis.service.user.UserService;

 /**
 * <p>描述：用户信息业务逻辑处理</p>
 * <p>公司：东华医为科技有限公司</p>
 * <p>版权：dhcc-2019</p>
 * @author liuyg
 * @since 2019-12-25
 */
@Component
public class UserBlh extends BaseAbstractBlh<UserDto> {

	private static Log logger = LogFactory.getLog(UserBlh.class);

	@Autowired
	public UserService userService;

	public UserBlh() {
		logger.debug("UserBlh Init");
	}
	
	/**
	 * 
	 * 进入某个列表的入口方法（分页查询方法）
	 * @param basedto
	 */
	public void list(BaseAbstractDto basedto) {
		UserDto dto = super.getExactlyDto(basedto);
		
		try {
			// 调用service查询方法
			Page<User> page = userService.list(dto);
			dto.setPage(page);
		} catch (Exception e) {
			logger.error(e);
			throw new DataBaseException(e.getMessage(), e);
		}
	}
	
	/**
	 * 
	 * 保存数据
	 * @param basedto
	 */
	public void save(BaseAbstractDto basedto) {
		UserDto dto = super.getExactlyDto(basedto);
		
		try {
			// 调用对应的service保存方法
//			userService.save(dto.getUser());
			userService.saveUserStudent(dto);
			dto.setOperateSuccess(BaseConstants.OPERATE_SUCCESS);
		} catch (Exception e) {
			logger.error(e);
			throw new DataBaseException(e.getMessage(), e);
		}
	}
	
	/**
	 * 
	  * 根据id更新数据
	 * @param basedto
	 */
	public void updateById(BaseAbstractDto basedto) {
		UserDto dto = super.getExactlyDto(basedto);
		
		try {
			// 调用对应的service更新方法
			userService.updateById(dto.getUser());
			dto.setOperateSuccess(BaseConstants.OPERATE_SUCCESS);
		} catch (Exception e) {
			logger.error(e);
			throw new DataBaseException(e.getMessage(), e);
		}
	}
	
	/**
	 * 
	  * 根据id删除数据
	 * @param basedto
	 */
	public void removeById(BaseAbstractDto basedto) {
		UserDto dto = super.getExactlyDto(basedto);
		
		try {
			// 调用对应的service删除方法
			userService.removeById(dto.getUser().getUserId());
			dto.setOperateSuccess(BaseConstants.OPERATE_SUCCESS);
		} catch (Exception e) {
			logger.error(e);
			throw new DataBaseException(e.getMessage(), e);
		}
	}
	
	/**
	 * 
	 * 根据id查询实体的方法
	 * @param: basedto
	 *  
	 */
	public void getById(BaseAbstractDto basedto) {
		UserDto dto = super.getExactlyDto(basedto);
		
		try {
			// 调用对应的service查询某个实体的方法
			User user = userService.getById(dto.getUser().getUserId()); 
			dto.setUser(user);
		} catch (Exception e) {
			logger.error(e);
			throw new DataBaseException(e.getMessage(), e);
		}
	}
	
	/**
	 * 
	 * 自定义XML查询示例
	 * @param: basedto
	 *  
	 */
	public void getUserById(BaseAbstractDto basedto) {
		UserDto dto = super.getExactlyDto(basedto);
		
		User user = userService.getUserById(dto);
		dto.setUser(user);
	}
}
